<?php
/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/8/24
 * Time: 14:00
 */

class Article extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    //访问文章
    public function show()
    {
        //获取文章id
        $artid=$this->input->get('artid');
        if (!isset($artid)||empty($artid)) die('article does not exist');
        //获取访问者id
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $this->load->model('kdtsalesman_model');
            $data = $this->kdtsalesman_model->showArticle($artid,$_SESSION['uid']);
            $this->load->view("salesman/article/".$data['model'],$data);
        }else{redirect('http://api.nldou.com/ucenter/weixinlogin?pid=6618&page=&param=artid.'.$artid);}
    }

    public function scan($model)
    {
        $this->load->model('kdtsalesman_model');
        $data=array('sls'=>'2cAhDI','type'=>'show','key'=>'','read'=>1);
        $this->load->view("salesman/article/".$model,$data);
    }

    //获取文章列表
    public function getArticles()
    {
        $post = $this->input->post();
        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->getArticles($offset,$limit);

        $this->success($res);
    }

    //生成文章
    public function create($model)
    {
        if (!isset($model)||empty($model)) die('request forbidden');
        $model=explode('.',$model);
        if (!isset($model[1])||empty($model[1])) die('request forbidden');

        $this->load->model('kdtsalesman_model');
        $key = $this->kdtsalesman_model->createArticle($model[0],$model[1]);

        if($key===false) die('request forbidden');

        $data=array("sls"=>$model[1],"type"=>"create","key"=>$key);
        $this->load->view("salesman/article/".$model[0],$data);
    }

    public function getArticleStatus()
    {
        $sls=$this->input->get('sls');
        if (!isset($sls)||empty($sls)) $this->error();

        $this->load->model('kdtsalesman_model');
        $articlestatus = $this->kdtsalesman_model->getArticleStatus($sls);
        $res=array();
        $res['num']=$articlestatus['num'];
        $res['read']=$articlestatus['read']['total'];
        $res['profit']=$articlestatus['profit']/100;
        $this->success($res);
    }

    //获取推文可提现金额
    public function getArticleRemainMoney()
    {
        $sls=$this->input->get('sls');
        if (!isset($sls)||empty($sls)) $this->error();

        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->getArticleStatus($sls);
        $totalMoney = $res['profit'];
        $totalDemandMoney = $this->kdtsalesman_model->getArticleTotalDemandMoney($sls);

        $remainMoney = ($totalMoney-$totalDemandMoney)/100;
        $this->success(array('remainmoney'=>$remainMoney));
    }

    //提现申请
    public function demandMoney()
    {
        $sls=$this->input->get('sls');
        $money=$this->input->get('money');//*100后的金额
        if (!isset($sls)||empty($sls)||!isset($money)||!is_numeric($money)) $this->error();

        $money=(int)$money;
        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->demandArticleMoney($sls,$money);
        $this->success($res);
    }

    public function getMoneyRecord()
    {
        $sls=$this->input->get('sls');
        if (!isset($sls)||empty($sls)) $this->error();

        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->getMoneyRecord($sls);
        $this->success($res);
    }

//    public function syncSalesmanArticle()
//    {
//        if (!is_cli()) die('request forbidden');
//        $this->load->model('kdtsalesman_model');
//        $this->kdtsalesman_model->syncSalesmanArticle();
//    }

}